var dbconfig = require('../util/dbconfig')

function rand(min,max) {
  return Math.floor(Math.random()*(max-min))
}

validatePhoneCode = [];
let sendCodeP = phone =>{
  for(var item of validatePhoneCode){
    if(phone===item.phone){
      return true
    }
  }
  return false
}
let findCodeAndPhone=(phone,code)=>{
  for(var item of validatePhoneCode){
    if(phone===item.phone && code ===item.code){
      return 'login'
    }
  }
  return 'error'
}

//检测验证码登录是否是第一次登录
let phoneLoginBind= async (phone)=>{
  let sql = 'select * from user where username=? or phone=?';
  let sqlArr = [phone,phone];
  let res = await dbconfig.SySqlConnect(sql,sqlArr);
  if(res.length){
    return res
  }else{
    //用户第一次注册，绑定表
    //用户注册
    //获取用户详情
  }
}
//用户注册
let regUser= async phone =>{
  let userpic='https://image.baidu.com/search/detail?ct=503316480&z=undefined&tn=baiduimagedetail&ipn=d&word=%E6%9D%8E%E7%9F%A5%E6%81%A9&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=undefined&hd=undefined&latest=undefined&copyright=undefined&cs=182240020,2944282303&os=121376104,3395721228&simid=0,0&pn=0&rn=1&di=74690&ln=875&fr=&fmq=1630545375849_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&is=0,0&istype=0&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%253A%252F%252Fc-ssl.duitang.com%252Fuploads%252Fitem%252F201912%252F21%252F20191221192805_dxhut.thumb.700_0.jpg%26refer%3Dhttp%253A%252F%252Fc-ssl.duitang.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1633137376%26t%3D53a4f98ae8da60d5e9d04649461d46e3&rpstart=0&rpnum=0&adpicid=0&nojc=undefined'
  let sql ='insert into user(username,userpic,phone,create_time) values(?,?,?,?)'
  let sqlArr = [phone,userpic,phone,(new Date()).valueOf]
  let res = await dbconfig.SySqlConnect(sql,sqlArr)
  if(res.affectedRows ==1){
    //执行成功获取用户信息
    //获取用户信息方法
    
  }
}



//模拟验证码发送接口
sendCode = (req,res)=>{
  let phone =req.body.phone
  if(sendCodeP(phone)){
    res.send({
      'code':400,
      'msg':'已经发送过验证码，稍后再发'
    })
  }
  let code = rand(1000,9999)
  validatePhoneCode.push({
    'phone':phone,
    'code':code
  })
  console.log(validatePhoneCode);
  res.send({
    'code':200,
    "msg":'发送成功'
  })
  console.log(code);
}

//验证码登录
codePhoneLogin = (req,res)=>{
  let {phone,code} = req.body;
  //该手机号是否发送过验证码
  if(sendCodeP(phone)){
    //验证码和手机是否匹配
    let status = findCodeAndPhone(phone,code);
    if(status ==='login'){
      res.send({
        'code':200,
        'msg':'登录成功'
      })
    }else if (status === 'error'){
      res.send({
        'code':400,
        'msg':'登录失败'
      })
    }
  }else{
    res.send({
      "code":400,
      'msg':'未发送验证码'
    })
  }
}

insert = (req,res)=>{
  let {id} = req.body;
  console.log(id);
  let sql = 'delete from `insert` where id=?';
  let sqlArr = [id];
  var callBack =(err,data)=>{
    if(err){
      console.log(err);
      res.send('出错了')
    }else{
      res.json({
        "list":data
      })
    }
  }
  dbconfig.sqlConnect(sql,sqlArr,callBack)
}

module.exports={
  sendCode,codePhoneLogin,insert
}